<?php
	// Look for matches in metadata
	$bd_matches = array();
	$dd_matches = array();
	
	for($i=0; $i<count($this->data); $i++)
	{
		$item = $this->data[$i];
		
		// Compare the birth/death dates
		if(isset($item->metadata->Birth_Date))
		{
			$item_date = $item->metadata->Birth_Date;
			
			if(!(strlen($item_date) == 4 && is_numeric($item_date)))
			{
				$time = strtotime($item->metadata->Birth_Date);
				
				if($time)
					$item_date = date("Y", $time);
			}
			
			if(!isset($bd_matches[$item_date]))
				$bd_matches[$item_date] = array();
				
			$bd_matches[$item_date][] = $item->id;
		}
		
		if(isset($item->metadata->Death_Date))
		{
			$item_date = $item->metadata->Death_Date;
			
			if(!(strlen($item_date) == 4 && is_numeric($item_date)))
			{
				$time = strtotime($item->metadata->Death_Date);
				
				if($time)
					$item_date = date("Y", $time);
			}
			
			if(!isset($dd_matches[$item_date]))
				$dd_matches[$item_date] = array();
				
			$dd_matches[$item_date][] = $item->id;
		}
	}
	
	// Invert the arrays so its ID => date
	$birthdates = array();
	$deathdates = array();
	
	foreach($bd_matches as $date => $array)
	{
		if(count($array) > 1)
		{
			foreach($array as $id)
				$birthdates[$id] = "bd-".$date;
		}
	}
	
	foreach($dd_matches as $date => $array)
	{
		if(count($array) > 1)
		{
			foreach($array as $id)
				$deathdates[$id] = "dd-".$date;
		}
	}
?>
<?php foreach($this->data as $item): ?>
	<div class="item" id="info-<?=$item->id?>">
		<h1><?=$item->label?></h1>
		<ul class="metadata">
			<li><span class="title">Source</span> <?=$item->metadata->X0?></li>
			<?php if(isset($item->metadata->Birth_Date)): ?>
				<li><span class="title">Birth Date</span> <span class="<?=(isset($birthdates[$item->id])?'metamatch '.$birthdates[$item->id]:'')?>"><?=$item->metadata->Birth_Date?></span></li>
			<?php endif; ?>
			<?php if(isset($item->metadata->Death_Date)): ?>
				<li><span class="title">Death Date</span> <span class="<?=(isset($deathdates[$item->id])?'metamatch '.$deathdates[$item->id]:'')?>"><?=$item->metadata->Death_Date?></span></li>
			<?php endif; ?>
			<?php if(isset($item->metadata->BirthPlace)): ?>
				<li><span class="title">Birth Place</span> <?=$item->metadata->BirthPlace?></li>
			<?php endif; ?>
			<?php if(isset($item->metadata->DeathPlace)): ?>
				<li><span class="title">Death Place</span> <?=$item->metadata->DeathPlace?></li>
			<?php endif; ?>
			<?php if(isset($item->metadata->Nationality)): ?>
				<li><span class="title">Nationality</span> <?=$item->metadata->Nationality?></li>
			<?php endif; ?>
			<?php if(isset($item->metadata->occupation)): ?>
				<li><span class="title">Occupation</span> <?=$item->metadata->occupation?></li>
			<?php endif; ?>
			<?php if(isset($item->metadata->Info)): ?>
				<li><span class="title">External Link</span> <a href="<?=$item->metadata->Info?>" target="_blank">original source</a></li>
			<?php endif; ?>
		</ul>
	</div>
<?php endforeach; ?>
<script type="text/javascript" charset="utf-8">
	$$('.metamatch').each(function(elem){
		var c = elem.className.replace('metamatch', '');
		c = c.replace('selected', '');
		c = c.replace(/\s/, '');
		
		elem.observe('mouseover', function(event){
			$$("#rhs .item ."+c).invoke('addClassName', 'selected');
		});
		
		elem.observe('mouseout', function(event){
			$$("#rhs .item ."+c).invoke('removeClassName', 'selected');
		});
	});
	
	$$('#infoview .item').each(function(elem){
		elem.observe('mouseover', function(event){
			var elem = event.target.hasClassName('item') ? event.target : event.target.up('.item');

			if(!elem.hasClassName('highlight'))
			{
				$$('.highlight').each(function(h){
					h.removeClassName('highlight');
				})
				
				$$("."+elem.id.replace("info-", "")).invoke("addClassName", "highlight");
				
				if($("grouped-"+elem.id.replace("info-", "")))
					$("grouped-"+elem.id.replace("info-", "")).addClassName("highlight");
			}
		});
		
		elem.observe('mouseout', function(event){
			var elem = event.target;
			if(!elem.hasClassName('item'))
				elem = event.target.up('.item');
			
			var pos = elem.cumulativeOffset();
			var dim = elem.getDimensions();
			
			if((event.pointerX() <= pos.left || event.pointerX() >= pos.left + dim.width) ||
			   (event.pointerY() <= pos.top || event.pointerY() >= pos.top + dim.height))
			{
				$$("."+elem.id.replace("info-", "")).invoke("removeClassName", "highlight");
				
				if($("grouped-"+elem.id.replace("info-", "")))
					$("grouped-"+elem.id.replace("info-", "")).removeClassName("highlight");
			}
		});
	});	
	
</script>